package com.aizuda.boot.modules.contract;

import com.aizuda.boot.modules.business.contract.service.ContractFieldService;
import com.aizuda.boot.modules.business.contractName.domain.request.GenerateNameDTO;
import com.aizuda.boot.modules.business.contractName.service.ContractNameRuleService;
import com.aizuda.boot.modules.business.contractNumber.domain.ContractNumberEvent;
import com.aizuda.boot.modules.business.contractNumber.domain.resquest.GenerateNumberDTO;
import com.aizuda.boot.modules.business.contractNumber.service.ContractNumberRuleService;
import com.aizuda.boot.modules.common.constant.RedisKeyConstant;
import com.aizuda.boot.modules.common.constant.enums.GenerateTimingEnums;
import com.aizuda.boot.modules.common.constant.enums.NameRuleType;
import com.aizuda.boot.modules.common.constant.enums.RuleTypeEnums;
import com.aizuda.boot.modules.common.util.RedisService;
import jakarta.annotation.Resource;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.context.ApplicationEventPublisher;

/**
 * @Author sangyirong
 * @ClassName NumberAndName
 * @Version 1.0
 * @Date 2025/7/9 18:40
 */
@SpringBootTest
public class NumberAndName {

    @Resource
    private ContractNameRuleService contractNameRuleService;

    @Resource
    private ContractNumberRuleService contractNumberRuleService;

    @Resource
    private ApplicationEventPublisher publisher;

    @Resource
    private RedisService redisService;

    @Resource
    private ContractFieldService contractFieldService;

    /**
     * 测试生成合同名称
     */
    @Test
    public void name(){
        GenerateNameDTO dto = new GenerateNameDTO();
//        dto.setLegalId(1);// 我方实体ID
        dto.setRuleType(NameRuleType.CONTRACT_APPLY);//申请合同
        dto.setFileName("文件主合同名称");
//        dto.setFileTitle("文件主合同标题");
//        dto.setContractTypeId(1); //合同类型编号
//        dto.setContractName("原合同名称");
//        dto.setDocumentType("收付款");// 收支类型
//        dto.setDepartmentId(1L);// 合同经办人部门ID
//        dto.setCounterpartyId(1);// 对方实体ID
        String contractName = contractNameRuleService.getContractName(dto);
        System.out.println(contractName);
    }


    /**
     * 测试生成合同编号
     * F-北京测试-202507-031-1123--ZZ03
     */
    @Test
    public void number(){
        GenerateNumberDTO dto = new GenerateNumberDTO();
        dto.setLegalId(3);
        dto.setRuleType(RuleTypeEnums.CONTRACT_APPLY);
        dto.setContractTypeId(6);
        dto.setDocumentType("收付款");
//        dto.setContractNumber("F-北京测试-202507-031");
        String contractNumber = contractNumberRuleService.getContractNumber(dto);
        System.out.println(contractNumber);
    }


    @Test
    public void eventTest(){
        ContractNumberEvent contractNumberEvent = new ContractNumberEvent();
        contractNumberEvent.setContractId(6331);
        contractNumberEvent.setGenerateTiming(GenerateTimingEnums.APPROVE);
        publisher.publishEvent(contractNumberEvent);
        System.out.println("发布完成");
    }

    @Test
    public void cache(){
        contractFieldService.list().forEach(item -> {
            redisService.deleteObject(RedisKeyConstant.Business.CONTRACT_FIELD_PREFIX + item.getCode());
        });
    }
}
